﻿using Common;
using Common.XmlHandle;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Globalization;
using Network.UpdateData;
using SFJDManagement.SocketHandle;

namespace SFJDManagement
{
    /// <summary>
    /// Page_Edit_Users.xaml 的交互逻辑
    /// </summary>
    public partial class Page_Edit_Users : Page
    {
        DataGrid dataGrid = new DataGrid();
        string preValue = "";

        public Page_Edit_Users()
        {
            if (!Runtime.config.IsAdmin)
                return;
            InitializeComponent();
            Ini();
        }

        public void Ini()
        {
            List<ClassConfig> list = Runtime.xmlUsers.GetAllUsers();
            dataGrid.ItemsSource = list;
            dataGrid.CanUserAddRows = false;
            dataGrid.CanUserDeleteRows = false;
            dataGrid.CanUserReorderColumns = false;
            dataGrid.Loaded += DataGrid_Loaded;
            dataGrid.BeginningEdit += DataGrid_BeginningEdit;
            dataGrid.CellEditEnding += DataGrid_CellEditEnding;
            
            Content = dataGrid;
        }

        private void DataGrid_Loaded(object sender, RoutedEventArgs e)
        {
            foreach(DataGridColumn column in dataGrid.Columns)
            {
                switch (column.Header.ToString())
                {
                    case "UserGuid":
                        column.IsReadOnly = true;
                        break;
                    case "OutPath":
                        column.IsReadOnly = true;
                        break;
                }
            }
        }

        private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            FrameworkElement element = e.Column.GetCellContent(e.Row);
            string value = element.GetValue();
            if (value == preValue)
            {
                return;
            }
            ClassConfig config = e.Row.DataContext as ClassConfig;
            switch (e.Column.Header.ToString())
            {
                case "UserGuid":
                    config.UserGuid = new Guid(value);
                    break;
                case "State":
                    config.State = Boolean.Parse(value);
                    break;
                case "UserName":
                    config.UserName = value;
                    break;
                case "UserLicense":
                    config.UserLicense = (License)Enum.Parse(typeof(License), value);
                    break;
                case "IsServer":
                    config.IsServer = Boolean.Parse(value);
                    break;
                case "IsAdmin":
                    config.IsAdmin = Boolean.Parse(value);
                    break;
                case "IsCustomize":
                    config.IsCustomize = Boolean.Parse(value);
                    break;
                case "IsQueryOnly":
                    config.IsQueryOnly = Boolean.Parse(value);
                    break;
                case "InPath":
                    config.InPath = value;
                    break;
            }
            config.OutPath = (e.Row.DataContext as ClassConfig).OutPath;
            Packet packet = new Packet(typeof(SC04EditUser).GetClient2ServerKey(), Runtime.config.UserGuid, config.Coding2String());
            CSendMsg.SendMsgNoWait(packet.ToBytes());
        }

        private void DataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
        {
            FrameworkElement element = e.Column.GetCellContent(e.Row);
            preValue = element.GetValue();
        }

        public class IOutpathColumn : DataGridTemplateColumn
        {
            protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
            {
                return base.GenerateEditingElement(cell, dataItem);
            }

            protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
            {
                return base.GenerateElement(cell, dataItem);
            }
        }

        public class ConverterOutPath : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                string b = "";
                foreach (string s in (value as List<string>))
                {
                    b += s + ",";
                }
                return b;
            }

            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                List<string> b = new List<string>(2);
                string[] str = (value as string).Split(',');
                foreach (string buff in str)
                {
                    if (!buff.Equals(""))
                    {
                        b.Add(buff);
                    }
                }
                return b;
            }
        }
    }
}
